﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HRMS_Model;
using System.Diagnostics;
namespace HRMS.Models
{
    public class ApplicantsModels
    {
        public static bool AddApplicant(Applicant applicant)
        {
            try
            {
                using (var appEntity = new HRMSEntities())
                {
                    appEntity.Applicants.AddObject(applicant);
                    appEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }
        public static bool DeleteApplicant(int id)
        {
            try
            {
                using (var appEntity = new HRMSEntities())
                {
                    appEntity.Applicants.DeleteObject(appEntity.Applicants.First(d => d.ApplicantId == id));
                    appEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }
        public static List<Applicant> GetApplicants(string sortExpression, string sortDirection, int pageIndex, int pageSize, int pageCount)
        {
            using (var appEntity = new HRMSEntities())
            {
                return appEntity.Applicants.Include("Degree").Include("Skill").OrderBy("it." + sortExpression + " " + sortDirection).Skip(pageIndex * pageSize).Take(pageCount * pageSize).ToList();
            }
        }

        public static int GetApplicantsCount()
        {
            using (var appEntity = new HRMSEntities())
            {
                return appEntity.Applicants.Count();
            }
        }

        public static bool UpdateApplicant(Applicant applicant)
        {
            try
            {
                using (var appEntity = new HRMSEntities())
                {
                    var applicantToEdit = appEntity.Applicants.FirstOrDefault(a => a.ApplicantId == applicant.ApplicantId);
                    if (applicantToEdit != null)
                    {
                        applicantToEdit.FirstName = applicant.FirstName;
                        applicantToEdit.MiddleName = applicant.MiddleName;
                        applicantToEdit.LastName = applicant.LastName;
                        applicantToEdit.Address = applicant.Address;
                        applicantToEdit.City = applicant.City;
                        applicantToEdit.State = applicant.State;
                        applicantToEdit.Zip = applicant.Zip;
                        applicantToEdit.MobileNo = applicant.MobileNo;
                        applicantToEdit.HomePhone = applicant.HomePhone;
                        applicantToEdit.Email = applicant.Email;
                        applicantToEdit.Gender = applicant.Gender;
                        applicantToEdit.DateOfBirth = applicant.DateOfBirth;
                        applicantToEdit.MartialStatus = applicant.MartialStatus;
                        applicantToEdit.DegreeId = applicant.DegreeId;
                        applicantToEdit.SkillId = applicant.SkillId;
                        applicantToEdit.Experience = applicant.Experience;
                        applicantToEdit.ResumeSize = applicant.ResumeSize;
                        applicantToEdit.Resume = applicant.Resume;
                    }
                    appEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }
    }
}

